package com.gx.gxreader.base

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.view.View

abstract class BaseActivity<V, out P : BasePresenter<V>> : AppCompatActivity() {

    private lateinit var mPresenter: P

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getContentViewId())
        //浅色标题栏字体
//        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        //深色标题栏字体
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        //沉浸式标题栏
        window.statusBarColor = Color.TRANSPARENT

        initViews()
        mPresenter = createPresenter()

        mPresenter.attachView(this as V)
        mPresenter.start()
    }

    protected abstract fun createPresenter(): P

    protected abstract fun getContentViewId() : Int

    abstract fun initViews()

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> FragmentTransaction) = beginTransaction().func().commit()

    fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int) = supportFragmentManager.inTransaction { add(frameId, fragment) }

    fun AppCompatActivity.replaceFragment(fragment: Fragment, frameId: Int) = supportFragmentManager.inTransaction{replace(frameId, fragment)}
}